home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / sipp / srgp / src / mac / srgp_col.c next >
Encoding:
C/C++ Source or Header  |  1992-09-11  |  7.0 KB  |  281 lines

  1. #include "HEADERS.h"
  2. #include "srgplocal.h"
  3. #include <assert.h>
  4. #include "ChooseWhichQuickDraw.h"
  5.  
  6. /** THIS FILE IS FOR MAC IMPLEMENTATION ONLY
  7. It contains conditionally compiled code based on the definition of
  8. the COLOR_QUICKDRAW macro.
  9. **/
  10.  
  11. #ifdef COLOR_QUICKDRAW
  12.     static ReqListRec **srgpmac__reqlist;
  13.     static CTabHandle srgpmac__savectab, srgpmac__applctab;
  14.  
  15.     static boolean applTableCurrentlyActivated = TRUE;
  16. #endif
  17.  
  18.  
  19.  
  20. void SRGP__activateApplColorTable (void)
  21. {
  22. #ifdef COLOR_QUICKDRAW
  23.    if (applTableCurrentlyActivated) return;
  24.    
  25.     SaveEntries (NULL, srgpmac__savectab, *srgpmac__reqlist);
  26.     RestoreEntries (srgpmac__applctab, NULL, *srgpmac__reqlist);
  27.     applTableCurrentlyActivated = TRUE;
  28. #endif
  29. }
  30.  
  31.  
  32.  
  33. void SRGP__deactivateApplColorTable (void)
  34. {
  35. #ifdef COLOR_QUICKDRAW
  36.    if ( ! applTableCurrentlyActivated) return;
  37.   
  38.     RestoreEntries (srgpmac__savectab, NULL, *srgpmac__reqlist);
  39.     applTableCurrentlyActivated = FALSE;
  40. #endif
  41. }
  42.  
  43.  
  44.  
  45. #ifdef COLOR_QUICKDRAW
  46. static void InitReqList (int first, int last, ReqListRec ***reqlistptr)
  47. {
  48.     register i;
  49.     
  50.     (*reqlistptr) = (ReqListRec**)
  51.         (NewHandle (sizeof(ReqListRec) + (last-first)*sizeof(short)));
  52.     (**reqlistptr)->reqLSize = last-first;
  53.     for (i=first; i<=last; i++)
  54.         (**reqlistptr)->reqLData[i-first] = i;
  55. }
  56.  
  57.  
  58. static void InitMacColorTable (int first, int last, CTabHandle *ctabptr)
  59. {
  60.     register i;
  61.      CTabHandle ctab;
  62.      
  63.     *ctabptr = ctab = (CTabHandle)
  64.        NewHandle (sizeof(ColorTable) + (last-first)*sizeof(ColorSpec));
  65.     (*ctab)->ctSize = last-first;
  66.     (*ctab)->ctSeed = GetCTSeed();
  67.     (*ctab)->ctFlags = (short) (((unsigned short)(-1))<<15);
  68.     for (i=first; i<=last; i++)
  69.         (*ctab)->ctTable[i-first].value = i;
  70. }
  71. #endif
  72.  
  73.  
  74. void SRGP_loadColorTable
  75.    (int startentry, int count,
  76.     unsigned short *redi, 
  77.     unsigned short *greeni,
  78.     unsigned short *bluei)
  79. {
  80.    register int i, j;
  81.    RGBColor rgb;
  82.    ReqListRec **rl;
  83.    CTabHandle ctab;
  84.  
  85.  
  86.    DEBUG_AIDS{
  87.       SRGP_trace (SRGP_logStream, "SRGP_loadColorTable  %d  %d  %x %x %x\n",
  88.           startentry, count, redi, greeni, bluei);
  89.  
  90.       /* PERFORM CHECKING LEGALITY OF THE RANGE OF INDICES. */
  91.       srgp_check_pixel_value (startentry, "start");
  92.       srgp_check_pixel_value (startentry+count-1, "end");
  93.       LeaveIfNonFatalErr();
  94.    }
  95.  
  96. #ifdef COLOR_QUICKDRAW
  97.    InitMacColorTable (startentry, startentry+count-1, &ctab);
  98.  
  99.    for (i=startentry,j=0; j<count; i++,j++){
  100.       rgb.red = redi[j];
  101.       rgb.green = greeni[j];
  102.       rgb.blue = bluei[j];
  103.       (**srgpmac__applctab).ctTable[i].rgb = rgb;
  104.       (**ctab).ctTable[j].rgb = rgb;
  105.    }
  106.    
  107.    InitReqList (startentry, startentry+count-1, &rl);
  108.     RestoreEntries (ctab, NULL, *rl);
  109.     DisposHandle (rl);
  110.     DisposHandle (ctab);
  111. #endif
  112. }
  113.  
  114.  
  115.  
  116.  
  117. void
  118. SRGP_inquireColorTable 
  119.    (int startentry, int count,
  120.     unsigned short *redi, 
  121.     unsigned short *greeni,
  122.     unsigned short *bluei)
  123. {
  124.    RGBColor rgb;
  125.    register i, j;
  126.  
  127.    DEBUG_AIDS{
  128.       /* PERFORM CHECKING LEGALITY OF THE RANGE OF INDICES. */
  129.       srgp_check_pixel_value (startentry, "start");
  130.       srgp_check_pixel_value (startentry+count-1, "end");
  131.       LeaveIfNonFatalErr();
  132.    }
  133.  
  134.  
  135. #ifdef COLOR_QUICKDRAW
  136.    for (i=startentry, j=0; j<count; i++,j++) {
  137.       rgb = (**srgpmac__applctab).ctTable[i].rgb;
  138.       redi[j]=rgb.red;
  139.       greeni[j]=rgb.green;
  140.       bluei[j]=rgb.blue;
  141.    }
  142. #endif
  143. }
  144.  
  145.  
  146.  
  147. #include <ctype.h>
  148.  
  149. void
  150. SRGP_loadCommonColor (entry, name)
  151. int entry;
  152. char *name;   /* Null-terminated string of characters */
  153. {
  154.    unsigned short r, g, b;
  155.    register i;
  156.    char colordescrstr[256], nameinlowercase[50], *cptr, *lcptr;
  157.    
  158.    DEBUG_AIDS{
  159.       SRGP_trace (SRGP_logStream, 
  160.           "SRGP_loadCommonColor  %d  %s\n", entry, name);
  161.       srgp_check_pixel_value (entry, "start/end");
  162.       LeaveIfNonFatalErr();
  163.    }
  164.    
  165. #ifdef COLOR_QUICKDRAW
  166.    for (cptr=name, lcptr=nameinlowercase; *cptr; cptr++)
  167.       if (*cptr != ' ')
  168.          *(lcptr++) = tolower(*cptr);
  169.    *lcptr = '\0';
  170.    
  171.    i=1;
  172.    while (1) {
  173.       GetIndString (colordescrstr, 130, i);
  174.       PtoCstr(colordescrstr);
  175.       if (strlen(colordescrstr)==0) {
  176.          /* OFF THE EDGE OF THE STRING LIST: bad color name */
  177.          SRGP__error (ERR_BAD_COLOR_NAME, name);
  178.          LeaveIfNonFatalErr();
  179.       }
  180.       else if (!strcmp(colordescrstr+12,nameinlowercase)) {
  181.          /* FOUND! */
  182.          assert (3==sscanf(colordescrstr, "%hu %hu %hu", &r, &g, &b));
  183.          r = r<<8;
  184.          g = g<<8;
  185.          b = b<<8;
  186.          PUSH_TRACE;
  187.          SRGP_loadColorTable (entry, 1, &r, &g, &b);
  188.          POP_TRACE;
  189.          break;
  190.       }
  191.       else
  192.          i++;
  193.    }
  194. #endif
  195. }
  196.  
  197.  
  198.  
  199. static char message[] =
  200.    "Insane parameter to SRGP_begin:  negative number of planes!";
  201.  
  202.  
  203. void 
  204. SRGP__initColor (int requested_planes)
  205. {
  206.    register i;
  207.    SysEnvRec theWorld;
  208.    
  209.    srgp__base_colorindex = 0;   /* always true */
  210.    SRGP_BLACK = 1;
  211.    SRGP_WHITE = 0;
  212.    
  213.    SysEnvirons (1, &theWorld);
  214.    
  215. #ifdef COLOR_QUICKDRAW
  216.    if ( ! theWorld.hasColorQD) {
  217.       StopAlert (14369, NULL);
  218.       exit (1);
  219.    }
  220.    
  221.    srgp__available_depth = (**(((CGrafPtr)srgpmac__cwindow)->portPixMap)).pixelSize;
  222.    
  223.    {
  224.       ColorSpec cspec;
  225.       int actual__max_pixel_value;
  226.       
  227.       if (requested_planes < 0)
  228.      ReportSpecialError (message, FATAL);
  229.  
  230.       if ((requested_planes == 0) || 
  231.       (requested_planes > srgp__available_depth)) 
  232.      srgp__application_depth = srgp__available_depth;
  233.       else
  234.      srgp__application_depth = requested_planes;
  235.       srgp__max_pixel_value =
  236.          actual__max_pixel_value = (1 << srgp__available_depth) - 1;
  237.       /* The above line seems to ignore the depth the appl asked for;
  238.          you'll see that later in this function I clean up. */
  239.       
  240.       
  241.       InitReqList (0, actual__max_pixel_value, &srgpmac__reqlist);
  242.       InitMacColorTable (0, actual__max_pixel_value, &srgpmac__savectab);
  243.       InitMacColorTable (0, actual__max_pixel_value, &srgpmac__applctab);
  244.       
  245.       SaveEntries (NULL, srgpmac__savectab, *srgpmac__reqlist);
  246.       
  247.       /* Only a few entries of application LUT are init'd.
  248.       /* Because of the Mac's non-support of XOR'ing pixel values, I must set
  249.          both edges of the actual (entire) color table, so at least xor'ing
  250.          1 ontop of 1 produces white, if the appl doesn't play with LUT entries 0,1 */
  251.       PUSH_TRACE;
  252.       
  253.       SRGP_loadCommonColor (0, "white");
  254.       SRGP_loadCommonColor (1, "black");
  255.       SRGP_loadCommonColor (actual__max_pixel_value, "black");
  256.       SRGP_loadCommonColor (actual__max_pixel_value-1, "white");
  257.       
  258.       srgp__max_pixel_value = (1 << srgp__application_depth) - 1;
  259.       
  260.       POP_TRACE;
  261.    }
  262. #else
  263.    if ((theWorld.hasColorQD) && (requested_planes > 1)) {
  264.       StopAlert (20065, NULL);
  265.       exit (1);
  266.    }
  267.    srgp__available_depth = 1;
  268.    srgp__application_depth = 1;
  269.    srgp__max_pixel_value = 1;
  270. #endif
  271. }
  272.  
  273.  
  274.  
  275.  
  276. void SRGP__cleanupColor()
  277. {
  278.     SRGP__deactivateApplColorTable ();
  279. }
  280.  
  281.